<?php
require_once 'BaseController.php';

class Admin_LoginController extends BaseController {
	public function checkUserLogin() {
		if($this->getRequest()->getActionName() != 'logout') {
			$this->admin_info = explode("\t",uc_authcode(CookieUtil::cookieGet("wenda_system")));
			$this->admin_info['uid'] = $this->admin_info[0];
			$this->admin_info['username'] = $this->admin_info[1];
			$this->admin_info['group_id'] = $this->admin_info[2];
			$this->admin_info['permission'] = $this->admin_info[3];
			
			$this->view->admin_info = $this->admin_info;
			if (!empty($this->admin_info['uid']) && !empty($this->admin_info['username'])) {
				$this->_redirect(__PROJECTNAME__ . '/admin');
			}
		}
	}
	
	public function indexAction(){
		try {
			if($_POST){
				$username = HttpUtil::postString('username');
				$password = HttpUtil::postString('password');
				if (empty($username) || empty($password)){
					$this->view->message = '用户名或密码不能为空!';
				}else {
					//通过接口判断登录帐号的正确性，返回值为数组
					list($uid, $username, $password, $email) = uc_user_login($username, $password);
					if($uid > 0) {
						setcookie('wenda_system', '', -86400 * 100000);
						$isLogin = $this->GetUserinfoByID($uid);
						//用户登陆成功，设置 Cookie，加密直接用 uc_authcode 函数，用户使用自己的函数
						//
						if ($isLogin[0]['user_id'] && $isLogin[0]['user_name']){
							setcookie('wenda_system', uc_authcode($isLogin[0]['user_id']."\t".$isLogin[0]['user_name']."\t".$isLogin[0]['group_id']."\t".$isLogin[0]['permission'], 'ENCODE'),0,'/');
							$user_infoDAO = $this->orm->createDAO('_users_info')->findUser_id($isLogin[0]['user_id']);
							$user_infoDAO->last_login = time();
							$user_infoDAO->last_ip = $_SERVER['REMOTE_ADDR'];
							$user_infoDAO->save();
							
							if(empty($_POST['redirect'])) {
								$this->_redirect('/admin/');
							}else {
								$this->_redirect($_POST['redirect']);
							}
							exit();
						}else {
							$this->view->message = '无此管理员账户';
						}
					} elseif($uid == -1) {
						$this->view->message = '用户不存在,或者被删除';
					} elseif($uid == -2) {
						$this->view->message = '密码错误';
					} else {
						$this->view->message = '未定义';
					}
				}
			}
			
			$this->view->title = '登录 - ';
			echo $this->view->render('login/index.htm');
			exit;
		}catch (Exception $e){
			$this->alert_back('错误：' . addslashes($e->getMessage() . '，请联系技术人员解决！'));
		}
	}
	
	public function logoutAction(){
		try {
			CookieUtil::cookieEnd("wenda_system");
			$this->_redirect(__PROJECTNAME__);
			exit;
		}catch (Exception $e){
			throw $e;
		}
	}
	
	//根据ID获取用户信息
	public function GetUserinfoByID($uid=null){
		try {
			$usr_infoDAO = $this->orm->createDAO('_users_info');
			$usr_infoDAO->with_users_group(array('group_id' => 'group_id'))->findGroup_type(1);
			$usr_infoDAO->findUser_id($uid);
			return $usr_infoDAO->get();
		}catch(Exception $e) {
			throw $e;
		}
	}
	
	//忘记密码
	public function forgetPasswordAction() {
		try {
			$email = HttpUtil::postString('email');
			if(!empty($email)) {
				$userInfoDAO = $this->orm->createDAO('_users_info')->findEmail($email);
				$userInfoDAO->with_users_group(array('group_id'=>'group_id'))->findGroup_type(1);
				$userInfo = $userInfoDAO->get();
				if(!empty($userInfo)) {
					$mid = uc_pm_send($userInfo[0]['user_id'],1,$userInfo[0]['user_name'] . ' 忘记密码',$userInfo[0]['user_id'] . ':' . $userInfo[0]['user_name'] . ':' . $userInfo[0]['email']);
					
					if($mid > 0) {
						$this->alert_back('已经申请管理员修改密码！');
					}else {
						$this->alert_back('申请发送失败！');
					}
				}else {
					$this->alert_back('无此管理员信息！');
				}
			}else {
				$this->alert_back('请输入邮箱！');
			}
		}catch(Exception $e) {
			$this->toErrorLogs($e);
			$this->alert_back(addslashes($e->getMessage()));
		}
	}
}

